<template>
  <div>
    <el-button type="primary" icon="el-icon-plus" @click="showDialog"
      >添加</el-button
    >
    <el-table style="width: 100%; margin-top: 20px" border :data="list">
      //第一列
      <el-table-column label="序号" width="100" align="center" type="index">
      </el-table-column>
      //第二列
      <el-table-column label="品牌名称" width="width" prop="tmName">
      </el-table-column>
      //第三列
      <el-table-column label="品牌LOGO" width="width">
        <template slot-scope="{ row }">
          <img :src="row.logoUrl" alt="" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      //第四列
      <el-table-column label="操作" width="width">
        <template slot-scope="{ row }">
          <el-button
            type="warning"
            size="mini"
            icon="el-icon-edit"
            @click="updateHanderle(row)"
            >修改</el-button
          >
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteTrademark(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 20px; text-align: center"
      layout="prev, pager, next, jumper, sizes,total"
      :total="total"
      :page-count="total"
      :page-size="limit"
      :page-sizes="[3, 5, 10]"
      :current-page="page"
      @current-change="changeCurrentPage"
      @size-change="sizeChange"
      @prev-click="prevPageChange"
      @next-click="nextPageChange"
    >
    </el-pagination>

    <!-- 对话框 -->
    <el-dialog
      :title="tmForm.id ? '修改品牌' : '添加品牌'"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="tmForm" :rules="rules" ref="tmName">
        <el-form-item label="品牌名称" label-width="15%" prop="tmName">
          <el-input autocomplete="off" v-model="tmForm.tmName"></el-input>
        </el-form-item>

        <el-form-item label="品牌logo" label-width="15%" prop="logoUrl">
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addORupdateTrademark"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>


<script>
export default {
  name: "Trademark",
  data() {
    return {
      //当前页
      page: 1,
      limit: 3,
      trademarkInfo: {},
      dialogFormVisible: false,
      formLabelWidth: "10%",
      imageUrl: "",
      tmForm: {
        tmName: "",
        logoUrl: "",
      },
      rules: {
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
          { min: 1, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        logoUrl: [
          { required: true, message: "请选logo图片", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    //获取品牌信息
    async getTrademark() {
      try {
        let result = await this.$API.trademark.reqTradeMark(
          this.page,
          this.limit
        );
        if (result.code == 200) {
          this.trademarkInfo = result.data;
        }
      } catch (error) {
        console.log(error);
      }
    },
    //改变当前页
    changeCurrentPage(page) {
      this.page = page;
      this.getTrademark(this.page, this.limit);
    },
    //改变每页现实的最大数目
    sizeChange(limit) {
      this.limit = limit;
      this.getTrademark(this.page, this.limit);
    },
    //上一页
    prevPageChange(page) {
      this.page = page - 1;
      this.getTrademark(this.page, this.limit);
    },
    //下一页
    nextPageChange(page) {
      this.page = page + 1;
      this.getTrademark(this.page, this.limit);
    },
    //是否显示对话框
    showDialog() {
      this.dialogFormVisible = true;
      this.tmForm = {
        tmName: "",
        logoUrl: "",
      };
    },
    //修改按钮
    updateHanderle(row) {
      this.dialogFormVisible = true;
      console.log(row);
      //深拷贝
      this.tmForm = { ...row };
      // this.tmForm.id = this.list[index].id;
      // this.tmForm.logoUrl = this.list[index].logoUrl;
      // this.tmForm.tmName = this.list[index].tmName;
    },
    //图片上传成功回调函数
    handleAvatarSuccess(res, file) {
      this.tmForm.logoUrl = res.data;
    },
    //图片上传前回调函数
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //添加品牌信息
    addORupdateTrademark() {
      this.$refs.tmName.validate(async (valid) => {
        if (valid) {
          if (this.tmForm.id) {
            let result = await this.$API.trademark.reqUpdateTrademark(
              this.tmForm
            );
            this.dialogFormVisible = false;
            this.getTrademark();
            if (result.code == 200) {
              this.$message({
                message: this.tmForm.id ? "修改品牌成功" : "添加品牌成功",
                type: "success",
              });
            }
          } else {
            let result = await this.$API.trademark.reqAddTrademark(this.tmForm);
            this.dialogFormVisible = false;
            this.$message({
              message: this.tmForm.id ? "修改品牌成功" : "添加品牌成功",
              type: "success",
            });
            this.getTrademark();
            console.log(result);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //删除品牌
    deleteTrademark(row) {
      this.$confirm(`是否删除品牌${row.tmName}`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          console.log(row.id, "成功");
          let result = await this.$API.trademark.reqDeleteTrademark(row.id);
          if (result.code === 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });

            console.log(this.list.length);
            let result2 = await this.$API.trademark.reqTradeMark(
              this.list.length - 1 < 1 ? this.page - 1 : this.page,
              this.limit
            );
            if (result2.code == 200) {
              this.page = this.list.length - 1 < 1 ? this.page - 1 : this.page;
              this.trademarkInfo = result2.data;
            }
          }
        })
        .catch(async () => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  computed: {
    //品牌列表
    list() {
      return this.trademarkInfo.records;
    },
    //品牌总条数
    total() {
      return this.trademarkInfo.total;
    },
  },
  mounted() {
    this.getTrademark();
  },
};
</script>


<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>